package com.solution.admin.order.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.order.entity.OrderPayEntity;
import com.solution.admin.order.service.OrderPayService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 订单付款web控制层
 * @author lilp
 * @date 2017-09-18 19:24:13
 */
@RequestMapping("/orderPay")
@Controller
public class OrderPayController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderPayController.class);

	@Autowired
	private OrderPayService orderPayService;
	
	/**
	 * 跳转订单付款查询页面
	 * @param OrderPay 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/toListOrderPayPage")
	public String toListOrderPayPage(OrderPayEntity orderPay, HttpServletRequest request) {
		return "order/orderPay_list";
	}

	/**
	 * 分页查询订单付款列表
	 * @param orderPay 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/listOrderPayPage")
	@ResponseBody
	public JSONGrid listOrderPayPage(OrderPayEntity orderPay, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = orderPayService.listOrderPayPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询订单付款失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	
	/**
	 * 查询订单付款列表
	 * @param orderPay 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/listOrderPay")
	@ResponseBody
	public JSONGrid listOrderPay(OrderPayEntity orderPay, HttpServletRequest request){
		String orderCode = request.getParameter("orderCode");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderCode", orderCode);
		paramMap.put("isDelete", PublicStatus.IS_DELETE.NORMAL.getId());
		try {
			List<OrderPayEntity> orderPayList = orderPayService.listOrderPay(paramMap);
			return new JSONGrid(orderPayList,orderPayList.size());
		} catch (Exception e) {
			handleException("查询订单付款失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/toAddOrderPay")
	public String toAddOrderPay(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderPay_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/toUpdateOrderPay")
	public String toUpdateOrderPay(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderPayEntity orderPay = orderPayService.getOrderPayById(id);
			request.setAttribute("orderPay", orderPay);
		} catch (Exception e) {
			handleException("跳转编辑订单付款页面错误", logger, e);
		}
		return "order/orderPay_form";
	}


	/**
	 * 新增或保存订单付款
	 * @param request 请求对象
	 * @param orderPayEntity 订单付款实体
	 * @return
	 * @author lilp
 	 * @date 2017-09-18 19:24:13
	 */
	@RequestMapping("/saveOrUpdateOrderPay")
	@ResponseBody
	public Map<String, Object> saveOrderPay(HttpServletRequest request, OrderPayEntity orderPayEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isEmpty(id)){
				result = orderPayService.updateOrderPay(orderPayEntity);
			}else{
				result = (int)orderPayService.saveOrderPay(orderPayEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加订单付款失败", logger, e);
		}
		return resultMap;
	}
	
}
